var express = require('express');
var path = require('path');
//var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var md5 = require('md5');
var request = require('request');

var routes = require('./routes/index');
var login = require('./routes/login');
var app_advertisement = require('./routes/app_advertisement');
var drivers_school_info = require('./routes/drivers_school_info');
var registration_methods = require('./routes/registration_methods');
var price_show = require('./routes/price_show');
var back_password = require('./routes/back_password');
var role_management = require('./routes/role_management');
var users_management = require('./routes/users_management');
var coach_info = require('./routes/coach_info');
var coach_evaluation = require('./routes/coach_evaluation');
var new_coach = require('./routes/new_coach');
var sale_personnel_info = require('./routes/sale_personnel_info');
var simulation_test = require('./routes/simulation_test');
var question_list = require('./routes/question_list');
var message_record = require('./routes/message_record');
var drivers_site_info = require('./routes/drivers_site_info');
var bulk_price_list = require('./routes/bulk_price_list');
var apply_order_list = require('./routes/apply_order_list');
var course_info_list = require('./routes/course_info_list');
var students_info_management = require('./routes/students_info_management');
var rebate_set = require('./routes/rebate_set');
var students_pay_list = require('./routes/students_pay_list');
var pay_settlement_list = require('./routes/pay_settlement_list');
var basic_info = require('./routes/basic_info');

var consts = require('./common/constants');
var wechat = require('wechat');
var config = {
    token: consts.TOKEN,
    appid: consts.AppID,
    encodingAESKey: '8CUzsBCUqBKnGhzLYNhmrN5cxbjCzrLbTkgPIJpNb4Q'
};

var app = express();

// view engine setup
var template = require('art-template');
template.config('base', '');
template.config('extname', '.html');
app.engine('.html', template.__express);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.query());

app.use('/', routes);
app.use('/login', login);
app.use('/app_advertisement', app_advertisement);
app.use('/drivers_school_info', drivers_school_info);
app.use('/registration_methods', registration_methods);
app.use('/price_show', price_show);
app.use('/back_password', back_password);
app.use('/role_management', role_management);
app.use('/users_management', users_management);
app.use('/coach_info', coach_info);
app.use('/coach_evaluation', coach_evaluation);
app.use('/new_coach', new_coach);
app.use('/sale_personnel_info', sale_personnel_info);
app.use('/simulation_test', simulation_test);
app.use('/question_list', question_list);
app.use('/message_record', message_record);
app.use('/drivers_site_info', drivers_site_info);
app.use('/bulk_price_list', bulk_price_list);
app.use('/apply_order_list', apply_order_list);
app.use('/course_info_list', course_info_list);
app.use('/students_info_management', students_info_management);
app.use('/rebate_set', rebate_set);
app.use('/students_pay_list', students_pay_list);
app.use('/pay_settlement_list', pay_settlement_list);
app.use('/basic_info', basic_info);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;
